﻿using System;
using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectIndieFarm
{
    public class ChallengeSystem : AbstractSystem, IChallengeSystem, IArchive
    {

        #region 胡萝卜
        /// <summary>
        /// 当天成熟的萝卜数量
        /// </summary>
        public static BindableProperty<int> RipeAndHarvestCountInCurrentDayCarrot { get; set; } = new(0);
        /// <summary>
        /// 当天收获的萝卜数量
        /// </summary>
        public static BindableProperty<int> HarvestCountInCurrentDayCarrot { get; set; } = new(0);
        /// <summary>
        /// 收获过的萝卜数量
        /// </summary>
        public static int HarvestedCountCarrot = 0;
        /// <summary>
        /// 道具栏中胡萝卜数量
        /// </summary>
        public static int ItemCountCarrot = 0;
        #endregion 胡萝卜

        #region 土豆
        /// <summary>
        /// 当天成熟的土豆数量
        /// </summary>
        public static BindableProperty<int> RipeAndHarvestCountInCurrentDayPotato { get; set; } = new(0);
        /// <summary>
        /// 当天收获的土豆数量
        /// </summary>
        public static BindableProperty<int> HarvestCountInCurrentDayPotato { get; set; } = new(0);
        /// <summary>
        /// 收获过的土豆数量
        /// </summary>
        public static int HarvestedCountPotato = 0;
        /// <summary>
        /// 道具栏中土豆数量
        /// </summary>
        public static int ItemCountPotato = 0;
        #endregion 土豆

        #region 西红柿
        /// <summary>
        /// 当天成熟的西红柿数量
        /// </summary>
        public static BindableProperty<int> RipeAndHarvestCountInCurrentDayTomato { get; set; } = new(0);
        /// <summary>
        /// 当天收获的西红柿数量
        /// </summary>
        public static BindableProperty<int> HarvestCountInCurrentDayTomato { get; set; } = new(0);
        /// <summary>
        /// 收获过的西红柿数量
        /// </summary>
        public static int HarvestedCountTomato = 0;
        /// <summary>
        /// 道具栏中西红柿数量
        /// </summary>
        public static int ItemCountTomato = 0;
        #endregion 西红柿

        #region 南瓜
        /// <summary>
        /// 当天成熟的南瓜数量
        /// </summary>
        public static BindableProperty<int> RipeAndHarvestCountInCurrentDayPumpkin { get; set; } = new(0);
        /// <summary>
        /// 当天收获的南瓜数量
        /// </summary>
        public static BindableProperty<int> HarvestCountInCurrentDayPumpkin { get; set; } = new(0);
        /// <summary>
        /// 收获过的南瓜数量
        /// </summary>
        public static int HarvestedCountPumpkin = 0;
        /// <summary>
        /// 道具栏中胡萝卜数量
        /// </summary>
        public static int ItemCountPumpkin = 0;
        #endregion 南瓜

        #region 豆角
        /// <summary>
        /// 当天成熟的豆角数量
        /// </summary>
        public static BindableProperty<int> RipeAndHarvestCountInCurrentDayBeans { get; set; } = new(0);
        /// <summary>
        /// 当天收获的豆角数量
        /// </summary>
        public static BindableProperty<int> HarvestCountInCurrentDayBeans { get; set; } = new(0);
        /// <summary>
        /// 收获过的豆角数量
        /// </summary>
        public static int HarvestedCountBeans = 0;
        /// <summary>
        /// 道具栏中胡萝卜数量
        /// </summary>
        public static int ItemCountBeans = 0;
        #endregion 豆角


        #region 挑战列表
        //挑战
        public List<Challenge> Challenges { get; } = new();
        public List<Challenge> ActiveChallenges { get; } = new();
        public List<Challenge> FinishChallenges { get; } = new();
        #endregion

        /// <summary>
        /// 挑战完成事件
        /// </summary>
        public static EasyEvent<Challenge> OnChallengeFinish { get; } = new();
        public static EasyEvent OnChallengeChanged { get; } = new();
        public static EasyEvent UpdateActiveDisplayText { get; } = new();



        protected override void OnInit()
        {
            Debug.Log($"challenge system oninit");
            LoadChallenges();
            //
            ToolbarSystem.OnItemCountChange.Register((item, count) =>
            {
                ItemCountChangeStats(item, count);
                UpdateActiveDisplayText.Trigger();
            });

            //监听 收获的作物是否为 当天成熟

            //监听成就的完成
            ActionKit.OnUpdate.Register(() =>
            {
                bool hasFinishChallenge = false;
                foreach (var challenge in ActiveChallenges)
                {

                    if (challenge.State == Challenge.States.Finished)
                    {
                        FinishChallenges.Add(challenge);
                        hasFinishChallenge = true;
                    }
                }
                //有挑战被完成 移除已完成的成就
                if (hasFinishChallenge)
                {
                    ActiveChallenges.RemoveAll(challenge => challenge.State == Challenge.States.Finished);
                }
                //活跃挑战列表为空 
                if (ActiveChallenges.Count == 0
                    && FinishChallenges.Count != Challenges.Count)
                {
                    //获取一个为完成的挑战 添加到活跃列表中
                    var randomItem = Challenges
                           .Where(challenge => challenge.State == Challenge.States.NotStart)
                           .ToList()
                           .First();
                    ActiveChallenges.Add(randomItem);
                    OnChallengeChanged.Trigger();
                    UpdateActiveDisplayText.Trigger();
                }
            });

            Global.OnPlantHarvest.Register((plant) =>
            {
                PlantHarvestStats(plant);
                UpdateActiveDisplayText.Trigger();
            });
        }
        /// <summary>
        /// 植物收获时成就相关数据更新
        /// </summary>
        /// <param name="plant"></param>
        private void PlantHarvestStats(IPlant plant)
        {
            Debug.Log($"  收获{plant.GetName()}");
            if (plant is Plant)
            {
                var plantObj = plant as Plant;
                var count = plantObj.SoilData.RipeCount;
                if (plantObj.Name == "potato")
                {
                    HarvestedCountPotato += count;
                    HarvestCountInCurrentDayPotato.Value += count;
                    if (plant.RipeDay == Global.Days.Value)
                    {
                        RipeAndHarvestCountInCurrentDayPotato.Value += count;
                    }
                }
                else if (plantObj.Name == "tomato")
                {
                    HarvestedCountTomato += count;
                    HarvestCountInCurrentDayTomato.Value += count;
                    if (plant.RipeDay == Global.Days.Value)
                    {
                        RipeAndHarvestCountInCurrentDayTomato.Value += count;
                    }
                }
                else if (plantObj.Name == "pumpkin")
                {
                    HarvestedCountPumpkin += count;
                    HarvestCountInCurrentDayPumpkin.Value += count;
                    if (plant.RipeDay == Global.Days.Value)
                    {
                        RipeAndHarvestCountInCurrentDayPumpkin.Value += count;
                    }
                }
                else if (plantObj.Name == "beans")
                {
                    HarvestedCountBeans += count;
                    HarvestCountInCurrentDayBeans.Value += count;
                    if (plant.RipeDay == Global.Days.Value)
                    {
                        RipeAndHarvestCountInCurrentDayBeans.Value += count;
                    }
                }
                else if (plantObj.Name == "carrot")
                {
                    HarvestedCountCarrot += count;
                    HarvestCountInCurrentDayCarrot.Value += count;
                    if (plant.RipeDay == Global.Days.Value)
                    {
                        RipeAndHarvestCountInCurrentDayCarrot.Value += count;
                    }
                }
            }
        }
        private void ItemCountChangeStats(ToolbarSlot item, int count)
        {
            if (item.ItemID.Value == "item_potato")
            {
                ItemCountPotato = count;
            }
            if (item.ItemID.Value == "item_carrot")
            {
                ItemCountCarrot = count;
            }
            else if (item.ItemID.Value == "item_tomato")
            {
                ItemCountTomato = count;
            }
            else if (item.ItemID.Value == "item_bean")
            {
                ItemCountBeans = count;
            }
            else if (item.ItemID.Value == "item_pumpkin")
            {
                ItemCountPumpkin = count;
            }
        }
        /// <summary>
        /// 初始化成就列表
        /// </summary>
        private void LoadChallenges()
        {
            Challenges.Clear();
            ActiveChallenges.Clear();
            FinishChallenges.Clear();
            Debug.Log($"challenge system LoadChallenges");

            ChallengeGroupPotato.Setup(this);
            ChallengeGroupCarrot.Setup(this);
            ChallengeGroupTomato.Setup(this);
            ChallengeGroupBeans.Setup(this);
            ChallengeGroupPumpkin.Setup(this);
            //二阶段流程
            ChallengeGroupAdditional.Setup(this);

        }

        public Challenge Add(Challenge challenge)
        {
            Challenges.Add(challenge);
            return challenge;
        }


        #region 存档相关
        /// <summary>
        /// 保存成就信息
        /// </summary>
        public void SaveData()
        {
            foreach (var challenge in Challenges)
            {
                PlayerPrefs.SetInt(challenge.Name, (int)challenge.State);
            }
            PlayerPrefs.SetInt(nameof(RipeAndHarvestCountInCurrentDayBeans), RipeAndHarvestCountInCurrentDayBeans.Value);
            PlayerPrefs.SetInt(nameof(RipeAndHarvestCountInCurrentDayCarrot), RipeAndHarvestCountInCurrentDayCarrot.Value);
            PlayerPrefs.SetInt(nameof(RipeAndHarvestCountInCurrentDayPotato), RipeAndHarvestCountInCurrentDayPotato.Value);
            PlayerPrefs.SetInt(nameof(RipeAndHarvestCountInCurrentDayPumpkin), RipeAndHarvestCountInCurrentDayPumpkin.Value);
            PlayerPrefs.SetInt(nameof(RipeAndHarvestCountInCurrentDayTomato), RipeAndHarvestCountInCurrentDayTomato.Value);

            PlayerPrefs.SetInt(nameof(HarvestCountInCurrentDayBeans), HarvestCountInCurrentDayBeans.Value);
            PlayerPrefs.SetInt(nameof(HarvestCountInCurrentDayCarrot), RipeAndHarvestCountInCurrentDayCarrot.Value);
            PlayerPrefs.SetInt(nameof(HarvestCountInCurrentDayPotato), HarvestCountInCurrentDayPotato.Value);
            PlayerPrefs.SetInt(nameof(HarvestCountInCurrentDayPumpkin), HarvestCountInCurrentDayPumpkin.Value);
            PlayerPrefs.SetInt(nameof(HarvestCountInCurrentDayTomato), HarvestCountInCurrentDayTomato.Value);

            PlayerPrefs.SetInt(nameof(HarvestedCountBeans), HarvestedCountBeans);
            PlayerPrefs.SetInt(nameof(HarvestedCountCarrot), HarvestedCountCarrot);
            PlayerPrefs.SetInt(nameof(HarvestedCountPotato), HarvestedCountPotato);
            PlayerPrefs.SetInt(nameof(HarvestedCountPumpkin), HarvestedCountPumpkin);
            PlayerPrefs.SetInt(nameof(HarvestedCountTomato), HarvestedCountTomato);

            PlayerPrefs.SetInt(nameof(ItemCountBeans), ItemCountBeans);
            PlayerPrefs.SetInt(nameof(ItemCountCarrot), ItemCountCarrot);
            PlayerPrefs.SetInt(nameof(ItemCountPotato), ItemCountPotato);
            PlayerPrefs.SetInt(nameof(ItemCountPumpkin), ItemCountPumpkin);
            PlayerPrefs.SetInt(nameof(ItemCountTomato), ItemCountTomato);
        }
        /// <summary>
        /// 加载成就信息
        /// </summary>
        public void LoadData()
        {
            foreach (var challenge in Challenges)
            {
                challenge.State = (Challenge.States)PlayerPrefs.GetInt(challenge.Name, (int)Challenge.States.NotStart);
                if (challenge.State == Challenge.States.Started)
                {
                    ActiveChallenges.Add(challenge);
                }
                else if (challenge.State == Challenge.States.Finished)
                {
                    FinishChallenges.Add(challenge);
                }
            }

            RipeAndHarvestCountInCurrentDayBeans.Value = PlayerPrefs.GetInt(nameof(RipeAndHarvestCountInCurrentDayBeans), 0);
            RipeAndHarvestCountInCurrentDayCarrot.Value = PlayerPrefs.GetInt(nameof(RipeAndHarvestCountInCurrentDayCarrot), 0);
            RipeAndHarvestCountInCurrentDayPotato.Value = PlayerPrefs.GetInt(nameof(RipeAndHarvestCountInCurrentDayPotato), 0);
            RipeAndHarvestCountInCurrentDayPumpkin.Value = PlayerPrefs.GetInt(nameof(RipeAndHarvestCountInCurrentDayPumpkin), 0);
            RipeAndHarvestCountInCurrentDayTomato.Value = PlayerPrefs.GetInt(nameof(RipeAndHarvestCountInCurrentDayTomato), 0);

            HarvestCountInCurrentDayBeans.Value = PlayerPrefs.GetInt(nameof(HarvestCountInCurrentDayBeans), 0);
            HarvestCountInCurrentDayCarrot.Value = PlayerPrefs.GetInt(nameof(HarvestCountInCurrentDayCarrot), 0);
            HarvestCountInCurrentDayPotato.Value = PlayerPrefs.GetInt(nameof(HarvestCountInCurrentDayPotato), 0);
            HarvestCountInCurrentDayPumpkin.Value = PlayerPrefs.GetInt(nameof(HarvestCountInCurrentDayPumpkin), 0);
            HarvestCountInCurrentDayTomato.Value = PlayerPrefs.GetInt(nameof(HarvestCountInCurrentDayTomato), 0);

            HarvestedCountBeans = PlayerPrefs.GetInt(nameof(HarvestedCountBeans), 0);
            HarvestedCountCarrot = PlayerPrefs.GetInt(nameof(HarvestedCountCarrot), 0);
            HarvestedCountPotato = PlayerPrefs.GetInt(nameof(HarvestedCountPotato), 0);
            HarvestedCountPumpkin = PlayerPrefs.GetInt(nameof(HarvestedCountPumpkin), 0);
            HarvestedCountTomato = PlayerPrefs.GetInt(nameof(HarvestedCountTomato), 0);

            ItemCountBeans = PlayerPrefs.GetInt(nameof(ItemCountBeans), 0);
            ItemCountCarrot = PlayerPrefs.GetInt(nameof(ItemCountCarrot), 0);
            ItemCountPotato = PlayerPrefs.GetInt(nameof(ItemCountPotato), 0);
            ItemCountPumpkin = PlayerPrefs.GetInt(nameof(ItemCountPumpkin), 0);
            ItemCountTomato = PlayerPrefs.GetInt(nameof(ItemCountTomato), 0);
        }
        /// <summary>
        /// 重置成就信息
        /// </summary>
        public void ResetData()
        {
            LoadChallenges();

            RipeAndHarvestCountInCurrentDayBeans.Value = 0;
            RipeAndHarvestCountInCurrentDayCarrot.Value = 0;
            RipeAndHarvestCountInCurrentDayPotato.Value = 0;
            RipeAndHarvestCountInCurrentDayPumpkin.Value = 0;
            RipeAndHarvestCountInCurrentDayTomato.Value = 0;

            HarvestCountInCurrentDayBeans.Value = 0;
            HarvestCountInCurrentDayCarrot.Value = 0;
            HarvestCountInCurrentDayPotato.Value = 0;
            HarvestCountInCurrentDayPumpkin.Value = 0;
            HarvestCountInCurrentDayTomato.Value = 0;

            HarvestedCountBeans = 0;
            HarvestedCountCarrot = 0;
            HarvestedCountPotato = 0;
            HarvestedCountPumpkin = 0;
            HarvestedCountTomato = 0;

            ItemCountBeans = 0;
            ItemCountCarrot = 0;
            ItemCountPotato = 0;
            ItemCountPumpkin = 0;
            ItemCountTomato = 0;
        }
        #endregion
    }
}
